using System;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace DevCow.Api.Meetup.Events
{
    [Serializable, XmlRoot("items")]
    public class MeetupEvents:IXmlSerializable
    {
        public List<MeetupEvent> Events { get; set; }

        public MeetupEvents()
        {
            Events = new List<MeetupEvent>();
        }

        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            throw new NotImplementedException();
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            while( reader.ReadToFollowing("item"))
            {
                MeetupEvent currentEvent = new MeetupEvent();

                XmlSerializer x =
                    new XmlSerializer(currentEvent.GetType());
                currentEvent = (MeetupEvent)x.Deserialize(reader);
                Events.Add(currentEvent);
            }
        }

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    #region Event Classes

    [Serializable, XmlRoot("item")]
    public class MeetupEvent
    {
        /// <summary>
        /// The ID of the event 
        /// </summary>
        [XmlElement("id")]
        public int ID { get; set; }

        /// <summary>
        /// The name (title) of the event
        /// </summary>
        [XmlElement("group_name")]
        public string Name { get; set; }

        /// <summary>
        /// The last date and time that the event's information was modified
        /// </summary>
        [XmlElement("updated")]
        public string Updated { get; set; }

        /// <summary>
        /// The date and time that the event will take place 
        /// </summary>
        [XmlElement("time")]
        public string Time { get; set; }

        /// <summary>
        /// URL of the event photo 
        /// </summary>
        [XmlElement("photo")]
        public string Photo { get; set; }

        /// <summary>
        /// Latitude and longitude coordinates of group location
        /// </summary>
        public string LatLon { get; set; }

        /// <summary>
        /// URL of the event's page on meetup.com 
        /// </summary>
        [XmlElement("event_url")]
        public string Url { get; set; }

        /// <summary>
        /// Current number of people who have rsvp'd "Yes" or "No" to this event.
        /// </summary>
        [XmlElement("rsvpcount")]
        public string RsvpCount { get; set; }

        /// <summary>
        /// Fee for this event (or 0.0 if this event has no fee) 
        /// </summary>
        [XmlElement("fee")]
        public string Fee { get; set; }

        /// <summary>
        /// Currency of the fee for this event 
        /// </summary>
        [XmlElement("feecurrency")]
        public string FeeCurrency { get; set; }

        /// <summary>
        /// Fee description, or blank if this event has no fee 
        /// </summary>
        [XmlElement("feedescription")]
        public string FeeDescription { get; set; }
    }

    public class MeetupEventRequest
    {
        private string currentRequest;

        private void AddParameter(string key, string value)
        {
            if (String.IsNullOrEmpty(currentRequest))
            {
                currentRequest = String.Format("?{0}={1}",key, value);
            }
            else
            {
                currentRequest += String.Format("&{0}={1}", key, value);
            }
        }

        public string GetRequestParameters()
        {
            return currentRequest;
        }

        #region Parameters
        /// <summary>
        /// A valid US zip code, limits the returned groups to those within radius miles. 
        /// </summary>
        public int Zip
        {
            set {AddParameter("zip",value.ToString());}
        }

        /// <summary>
        /// country, city, state A valid country code, city and for the US, State. limits the returned groups to those within radius miles. 
        /// </summary>
        public int Location { get; set; }

        /// <summary>
        /// //lat, lon A valid latitude and longitude, limits the returned groups to those within radius miles 
        /// </summary>
        public int LatLong { get; set; }

        /// <summary>
        /// Return events in the specified topic(s) [one topic allowed] 
        /// </summary>
        public string Topic { set { AddParameter("topic", value); } }

        /// <summary>
        /// Return events with the specified ID's [separate ID's keys with commas] 
        /// </summary>
        public int ID { set { AddParameter("id", value.ToString()); } }

        /// <summary>
        /// Return events from groups with the specified ID's [separate ID's keys with commas] 
        /// </summary>
        public int GroupId { set { AddParameter("group_id", value.ToString()); } }

        /// <summary>
        /// Return events for the group with given topic and number 
        /// </summary>
        public string TopicGroupNum { get; set; }

        /// <summary>
        /// Return events for the group with this custom URL path 
        /// </summary>
        public string GroupUrlName { set { AddParameter("group_urlname", value.ToString()); } }

        /// <summary>
        /// Return events that start after the specified date, format MMDDYYYY (if the time range includes past events, you must specify an event id, group id, or groupnum and topic) 
        /// </summary>
        public DateTime After { get; set; }

        /// <summary>
        /// Return events that started before the specified date, format MMDDYYYY (limited to one year beyond the after date, and if this includes past events you must specify an event id, group ud, or groupnum + topic) 
        /// </summary>
        public DateTime Before { get; set; }

        /// <summary>
        /// Radius, in miles for geographic requests, default 25 -- maximum 100 
        /// </summary>
        public int Radius { set { AddParameter("radius", value.ToString()); } }
        #endregion

        #region Ordering Parameters

        /// <summary>
        /// event time 
        /// </summary>
        public bool OrderByTime { get; set; }

        /// <summary>
        /// the name of the group having the event 
        /// </summary>
        public bool OrderByGroup { get; set; }

        /// <summary>
        /// the topic of the event 
        /// </summary>
        public bool OrderByTopic { get; set; }

        /// <summary>
        /// //location group location, country, state [if present], city 
        /// </summary>
        public bool OrderByLocation { get; set; }
        #endregion
    }
    #endregion
}